package com.itheima02;

import com.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Xiao
 * @date 2021/7/6
 */
public class Print_ArrayList03 {
    /*
        键盘录入学生信息到集合：
            创建一个存储学生对象的集合，存储3个学生对象，实用程序实现在控制台遍历该集合
            学生的姓名和年龄来自于键盘导入；
     */
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        Student stu1 = getStudent(sc);
        Student stu2 = getStudent(sc);
        Student stu3 = getStudent(sc);
        System.out.println("-------------");
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        printArryList(list);
    }

    private static void printArryList(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName() + "\t" + stu.getAge());
        }
    }

    private static Student getStudent(Scanner sc) {
        Student stu = new Student();
        System.out.println("请输入学生姓名：");
        String name =sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        stu.setName(name);
        stu.setAge(age);
        return stu;
    }
}
